package com.lanou.bbs.controller;

import com.lanou.bbs.entity.BbsFloor;
import com.lanou.bbs.entity.FloorAndUser;
import com.lanou.bbs.entity.User;
import com.lanou.bbs.service.FloorService;
import com.lanou.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 张贵宏
 * @Date 18/6/27 上午9:35
 */
@RequestMapping("/floor")
@Controller
public class FloorController {
    @Autowired
    private FloorService floorService;
    @Autowired
    private UserService userService;

    @RequestMapping("/sendFloor")
    @ResponseBody
    public void sendFloor(BbsFloor bbsFloor, HttpSession session) {
        User user = (User) session.getAttribute("user");

        bbsFloor.setUid(user.getUid());

        floorService.sendFloor(bbsFloor);


    }

    @RequestMapping("/findByRid")
    @ResponseBody
    private List<FloorAndUser> findByRid(int rid) {
        List<FloorAndUser> floorAndUserList = new ArrayList<>();
        List<BbsFloor> bbsFloorList = floorService.findByRid(rid);
        for (BbsFloor bbsFloor : bbsFloorList) {
            FloorAndUser floorAndUser = new FloorAndUser();
            User user = userService.findById(bbsFloor.getUid());
            floorAndUserList.add(floorAndUser.setBbsFloor(bbsFloor).setUser(user));
        }

        return floorAndUserList;
    }

}
